package com.gzbugu.polytechsasia.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA. User: Bokchoy Date: 11/2/12 Time: 5:05 PM
 */
public class LoginFilter implements Filter {
	private static Logger log = LoggerFactory.getLogger(LoginFilter.class);

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
	}

	@Override
	public void destroy() {
	}

	@Override
	public void doFilter(ServletRequest servletRequest,
			ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException {
		log.debug("test session");
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse respone = (HttpServletResponse) servletResponse;
		HttpSession session = request.getSession();
		String flag = (String) session.getAttribute("USER_ID");
		if ("".equals(flag) || flag == null) {
			log.debug("session is timeout");
			respone.sendRedirect(request.getContextPath());
		} else {
			filterChain.doFilter(servletRequest, servletResponse);
		}
	}
}
